import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.AbstractAction;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;



public class CreateGroupFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JFileChooser fileChooser;
	private final FileChooserListener fileChooserListener=new FileChooserListener();
	private JPanel panel_1;
	private JButton btnOpen;
	private JLabel lblNewLabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CreateGroupFrame frame = new CreateGroupFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CreateGroupFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 651, 380);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 50, 576, 269);
		contentPane.add(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("From Existing Contacts", null, panel, null);
		panel.setLayout(null);
		
		JLabel lblGroupName = new JLabel("Group Name");
		lblGroupName.setBounds(10, 11, 81, 14);
		panel.add(lblGroupName);
		
		textField = new JTextField();
		textField.setBounds(186, 8, 196, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		JList list = new JList();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setModel(new AbstractListModel() {
			String[] values = new String[] {"Ankit", "Sumit"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		list.setSelectedIndex(0);
		list.setBounds(10, 89, 109, 130);
		panel.add(list);
		
		JLabel lblContacts = new JLabel("Contacts");
		lblContacts.setBounds(10, 64, 69, 14);
		panel.add(lblContacts);
		
		textField_1 = new JTextField();
		textField_1.setBounds(183, 83, 336, 20);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnCreateGroup = new JButton("Create Group");
		btnCreateGroup.setBounds(217, 194, 109, 23);
		panel.add(btnCreateGroup);
		
		panel_1 = new JPanel();
		tabbedPane.addTab("By importing file", null, panel_1, "By importing file");
		panel_1.setLayout(null);
		
		JLabel lblGroupName_1 = new JLabel("Group Name");
		lblGroupName_1.setBounds(10, 23, 79, 14);
		panel_1.add(lblGroupName_1);
		
		JLabel lblBrowse = new JLabel("Browse...");
		lblBrowse.setBounds(10, 92, 67, 14);
		panel_1.add(lblBrowse);
		
		textField_2 = new JTextField();
		textField_2.setBounds(119, 20, 154, 20);
		panel_1.add(textField_2);
		textField_2.setColumns(10);
		
		btnOpen = new JButton("Open...");
		btnOpen.addActionListener(fileChooserListener);
		btnOpen.setBounds(119, 88, 89, 23);
		panel_1.add(btnOpen);
		
		lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(227, 92, 303, 14);
		lblNewLabel.setVisible(false);
		panel_1.add(lblNewLabel);
		
		JButton btnCreateGroup_1 = new JButton("Create Group");
		btnCreateGroup_1.setBounds(78, 165, 130, 23);
		panel_1.add(btnCreateGroup_1);
		
		fileChooser=new JFileChooser();
		fileChooser.addActionListener(fileChooserListener);
		
		
		
	}
	private class FileChooserListener implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==btnOpen){
			fileChooser.showOpenDialog(panel_1);
			}
			if(e.getSource()==fileChooser){
				if(e.getActionCommand()==JFileChooser.APPROVE_SELECTION){
					try{
					lblNewLabel.setText(fileChooser.getSelectedFile().getName());
					lblNewLabel.setToolTipText(fileChooser.getSelectedFile().getCanonicalPath());
					lblNewLabel.setVisible(true);					
					System.out.println(fileChooser.getSelectedFile());
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			}
		}
	}
}
